#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/types.h>
#include <linux/input.h>

#include "../public.h"

#define INPUT_EVENT0 "/dev/input/event0"

static int fd_keypad;

int KEY_Init()
{
    if ((fd_keypad = open(INPUT_EVENT0, O_RDONLY | O_NONBLOCK)) < 0)
    {
        return -1;
    }

    return 0;
}

void KEY_GetKeyValue(ST_KEY_INFO *pstKeyInfo)
{
    struct timeval tv = {0, 0}; // 非阻塞select
    struct input_event key;
    fd_set rfds;
    int retval, maxfd;
    unsigned short user_key_code = 0xFFFF;

    FD_ZERO(&rfds);
    FD_SET(fd_keypad, &rfds);
    maxfd = fd_keypad;
    retval = select(maxfd + 1, &rfds, NULL, NULL, &tv);

    if (0 >= retval)
    {
        return;
    }

    if (FD_ISSET(fd_keypad, &rfds))
    {
        if (0 < read(fd_keypad, &key, sizeof(struct input_event)))
        {
            if (key.type)
            {
                switch (key.code)
                {
                case 17:
                    user_key_code = EN_USER_KEY_0;
                    break;
                case 29:
                    user_key_code = EN_USER_KEY_1;
                    break;
                case 20:
                    user_key_code = EN_USER_KEY_2;
                    break;
                case 12:
                    user_key_code = EN_USER_KEY_3;
                    break;
                case 27:
                    user_key_code = EN_USER_KEY_4;
                    break;
                case 19:
                    user_key_code = EN_USER_KEY_5;
                    break;
                case 11:
                    user_key_code = EN_USER_KEY_6;
                    break;
                case 26:
                    user_key_code = EN_USER_KEY_7;
                    break;
                case 18:
                    user_key_code = EN_USER_KEY_8;
                    break;
                case 10:
                    user_key_code = EN_USER_KEY_9;
                    break;
                case 25:
                    user_key_code = EN_USER_KEY_STAR;
                    break;
                case 9:
                    user_key_code = EN_USER_KEY_POUND;
                    break;
                case 2:
                    user_key_code = EN_USER_KEY_REDIAL;
                    break;
                case 1:
                    user_key_code = EN_USER_KEY_SPEAKER;
                    break;
                case 33:
                    user_key_code = EN_USER_KEY_MEM3;
                    break;
                case 37:
                    user_key_code = EN_USER_KEY_MEM2;
                    break;
                case 34:
                    user_key_code = EN_USER_KEY_MEM1;
                    break;
                case 42:
                    user_key_code = EN_USER_KEY_SOFT1;
                    break;
                case 45:
                    user_key_code = EN_USER_KEY_SOFT2;
                    break;
                case 43:
                    user_key_code = EN_USER_KEY_SOFT3;
                    break;
                case 41:
                    user_key_code = EN_USER_KEY_SOFT4;
                    break;
                case 14:
                    user_key_code = EN_USER_KEY_VOLINC;
                    break;
                case 6:
                    user_key_code = EN_USER_KEY_VOLDEC;
                    break;
                case 5:
                    user_key_code = EN_USER_KEY_MUTING;
                    break;
                case 35:
                    user_key_code = EN_USER_KEY_UP;
                    break;
                case 21:
                    user_key_code = EN_USER_KEY_DOWN;
                    break;
                case 44:
                    user_key_code = EN_USER_KEY_LEFT;
                    break;
                case 13:
                    user_key_code = EN_USER_KEY_RIGHT;
                    break;
                case 36:
                    user_key_code = EN_USER_KEY_OK;
                    break;
                case 3:
                    user_key_code = EN_USER_KEY_HEADSET;
                    break;
                case 4:
                    user_key_code = EN_USER_KEY_MESSAGE;
                    break;

                default:
                    user_key_code = EN_USER_KEY_INVALID;
                    break;
                }

                printf("Code: %d --> UserKey: %d --> %s\n", key.code, user_key_code, key.value ? "Pressed" : "Released");

                if (user_key_code < EN_USER_KEY_INVALID)
                {
                    pstKeyInfo->code = user_key_code;
                    pstKeyInfo->value = (unsigned short)key.value;
                }
            }
        }
    }
}

void KEY_Exit()
{
    close(fd_keypad);
}
